/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* --------
* Table A9
* --------

* Globals
global dep1 "risk tax_rate red_i mixed_dec differentiated valitrade_advice"
global dep2 "ln_ival_usd ln_iwei nup_gap_i_int htrl_i_int e20"

* Matrix
mat tableA9_1 = J(4,6,.)
mat tableA9_1T = J(1,6,.)
mat tableA9_2 = J(4,5,.)
mat tableA9_2T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep1 {

	qui: reghdfe `x' eis_nr_f if sample_reg==1 , noab vce(cluster insp_f key_dec)
	
	mat tableA9_1[`i',`j'] = _b[eis_nr_f]
	local i = `i' + 1
	mat tableA9_1[`i',`j'] = _se[eis_nr_f]
	mat tableA9_1T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA9_1[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}

local i = 1
local j = 1
foreach x in $dep2 {

	qui: reghdfe `x' eis_nr_f if sample_reg==1 , noab vce(cluster insp_f key_dec)
	
	mat tableA9_2[`i',`j'] = _b[eis_nr_f]
	local i = `i' + 1
	mat tableA9_2[`i',`j'] = _se[eis_nr_f]
	mat tableA9_2T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA9_2[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A9.xlsx", replace sheet(TableA9) // create a new excel spreadsheet

* Coefficients
putexcel B4 = matrix(tableA9_1), nformat(0.000) hcenter vcenter
putexcel B6 = matrix(tableA9_1T), nformat(number_sep) hcenter vcenter
putexcel C11 = matrix(tableA9_2), nformat(0.000) hcenter vcenter
putexcel C13 = matrix(tableA9_2T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:G1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* First part
putexcel A2 = "Dependent variable:", left vcenter bold
putexcel A4 = "Excess interaction share - inspector logits", left vcenter
putexcel A6 = "Observations", left vcenter
putexcel A7 = "R-squared", left vcenter
local cells `" "B" "C" "D" "E" "F" "G" "'
local labels `" "Risk score" "Tax rate" "Red channel dummy" "Mixed shipment dummy" "Differentiated share" "Valuation advice dummy" "'
forvalues i = 1(1)6 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
}

* Second part
putexcel A9 = "Dependent variable:", left vcenter bold
putexcel A11 = "Excess interaction share - inspector logits", left vcenter
putexcel A13 = "Observations", left vcenter
putexcel A14 = "R-squared", left vcenter
local cells `" "C" "D" "E" "F" "G" "'
local labels `" "Log initial value" "Log initial weight" "Log initial unit price" "Initial hyp. tax rev. losses" "High potential tax yield dummy" "'
local j = 7
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'9 = "`x'", hcenter vcenter bold
	putexcel `y'10 = "(`j')", hcenter vcenter
	local j = `j'+1
}


* -------------------------------- ENDS HERE -------------------------------- *